﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/14
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Components.MikeLoggor
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using MikeWare.Core.Extensions;

    public partial class Loggor : IDisposable
    {
        public IList<Listener> Listeners { get; set; }
        public IList<Settings> GlobalSettings { get; private set; }

        public Loggor(String settingsFilePath) : this(SettingsReader.Read(settingsFilePath)) { }
        public Loggor(IList<Settings> settings)
        {
            GlobalSettings = settings;

            if (null == GlobalSettings || 0 == GlobalSettings.Count)
            {
                /// TODO: Load Default Settings
                throw new NotImplementedException();
            }

            foreach (var setting in GlobalSettings)
            {
                Trace.WriteLine(setting.ToString());
            }
            Listeners = Listener.BuildListeners(this, GlobalSettings);
        }

        public void Flush()
        {
            if (null != Listeners)
                foreach (var listener in Listeners)
                    listener.Flush();
        }
        public void Close()
        {
            if (null != Listeners)
                foreach (var listener in Listeners)
                    listener.Close();
        }
        public void Dispose()
        {
            if (null != Listeners)
            {
                foreach (var listener in Listeners)
                {
                    listener.Close();
                }
                Listeners.Clear();
                Listeners = null;
            }
        }

        public void Write(String message)
        {
            var now = DateTime.Now;
            if (null != Listeners)
                foreach (var listener in Listeners)
                    listener.Write(now, message);
        }
        public void WriteLine(String message)
        {
            Write(message + Environment.NewLine);
        }
        public void WriteLine()
        {
            if (null != Listeners)
                foreach (var listener in Listeners)
                    listener.WriteLine();
        }
        public void Write(Exception exception)
        {
            Write(exception, null);
        }
        public void Write(Exception exception, string title)
        {
            var message = FormatException(exception, title);
            Write(message);
        }
        public void WriteLine(Exception exception)
        {
            WriteLine(exception, null);
        }
        public void WriteLine(Exception exception, string title)
        {
            Write(exception, title);
            WriteLine();
        }

        private string FormatException(Exception exception, string title)
        {
            return exception.GetExceptionContent(title);
        }
    }
}
